local love = require "love"
require "conf"

local DRAG_COEFFICIENT = 0.2    -- 0 ~ 1
local MAX_VELOCITY = 300
local THRUST_ACCELERATION = 10000
local FLAME_TIME = 1    -- 0 ~ 1

local Ship = {}

function Ship.new(self, object)
    object = object or {}
    self.__index = self
    setmetatable(object, self)

    object.x = love.graphics.getWidth() / 2
    object.y = love.graphics.getHeight() / 2
    object.radius = 20  -- hitbox is a circle
    object.view_angle = math.rad(90)    -- counterclockwise
    object.rotate_angle = 0
    object.velocity = 0
    object.flame_time = 0
    object.survive = true
    object.thrust = false

    return object
end

function Ship.move(self)
    local dt = love.timer.getDelta()
    self.rotate_angle = math.rad(270) * dt

    -- rotate
    if love.keyboard.isDown(SHIP_ROTATE_LEFT) then
        self.view_angle = self.view_angle + self.rotate_angle
    elseif love.keyboard.isDown(SHIP_ROTATE_RIGHT) then
        self.view_angle = self.view_angle - self.rotate_angle
    end

    -- thrust
    if love.keyboard.isDown(SIHP_THRUST) then
        if self.velocity < MAX_VELOCITY then
            self.velocity = self.velocity + THRUST_ACCELERATION * dt
        end

        if  self.velocity > MAX_VELOCITY then
            self.velocity = MAX_VELOCITY
        end

        self.flame_time = FLAME_TIME
        self.thrust = true
    else
        self.thrust = false
    end

    -- update the location
    self.x = self.x + math.cos(self.view_angle) * self.velocity * dt
    self.y = self.y - math.sin(self.view_angle) * self.velocity * dt

    -- boundary limit
    if self.x + self.radius <= 0 then
        self.x = love.graphics.getWidth() + self.radius
    elseif self.x >= love.graphics.getWidth() + self.radius then
        self.x = - self.radius
    end

    if self.y + self.radius <= 0 then
        self.y = love.graphics.getHeight() + self.radius
    elseif self.y >= love.graphics.getHeight() + self.radius then
        self.y = - self.radius
    end

    -- slow down
    local drag_acceleration = DRAG_COEFFICIENT * self.velocity
    self.velocity = self.velocity - drag_acceleration * dt
    if self.velocity < 0 then self.velocity = 0 end

    self.flame_time = self.flame_time - love.timer.getDelta()
    if self.flame_time < 0 then self.flame_time = 0 end
end

function Ship.draw(self)
    -- draw the ship
    local vertices = {
        self.x + self.radius * math.cos(self.view_angle),
        self.y - self.radius * math.sin(self.view_angle),

        self.x + self.radius * math.cos(self.view_angle + math.rad(120)),
        self.y - self.radius * math.sin(self.view_angle + math.rad(120)),

        self.x,
        self.y,

        self.x + self.radius * math.cos(self.view_angle + math.rad(240)),
        self.y - self.radius * math.sin(self.view_angle + math.rad(240))
    }

    love.graphics.setColor(1, 1, 1)
    love.graphics.polygon("fill", vertices)

    -- draw the flame
    if self.flame_time ~= 0 then
        love.graphics.setColor(self.flame_time / FLAME_TIME, 0, 0)
        love.graphics.arc("fill", self.x, self.y, 2 / 3 * self.radius,
            math.rad(120) - self.view_angle, math.rad(240) - self.view_angle)
    end
end

return Ship
